package com.xjlsoft.weather.filter;


import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@Component
public class CorsFilter implements Filter {

    
     @Override
     public void init(FilterConfig filterConfig) throws ServletException {
    
     }
    
     @Override
     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
         HttpServletResponse httpResponse = (HttpServletResponse) response;
         HttpServletRequest httpRequest = (HttpServletRequest) request;
         String orgin=httpRequest.getHeader("Origin");
         String method=httpRequest.getMethod();
         String Headers=httpRequest.getHeader("Access-Control-Request-Headers");
         String token=((HttpServletRequest)request).getHeader("token");
         Enumeration<String> str=((HttpServletRequest)request).getHeaders("token");
          String testy= str.toString();
         httpResponse.setHeader("Access-Control-Allow-Origin", orgin);
         httpResponse.setHeader("Access-Control-Allow-Methods", method);
         httpResponse.setHeader("Access-Control-Max-Age", "3600");
         httpResponse.setHeader("Access-Control-Allow-Headers", Headers);
         chain.doFilter(request, response);
     }
    
     @Override
     public void destroy() {
    
     }
}